﻿using Swsk33.BasicGISGDI.Model;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Swsk33.BasicGISGDI.Util
{
	/// <summary>
	/// 用于在画布上画图的实用类
	/// </summary>
	public class ElementPainter
	{
		/// <summary>
		/// 绘制线段的笔实例
		/// </summary>
		private Pen linePen = new Pen(Color.Blue, 1.5f);

		/// <summary>
		/// 绘制点的笔刷实例
		/// </summary>
		private Brush pointBrush = new SolidBrush(Color.Red);

		/// <summary>
		/// 要绘制的画布
		/// </summary>
		private Graphics graphics;

		/// <summary>
		/// 构造器依赖注入
		/// </summary>
		/// <param name="graphics">传入画布对象</param>
		public ElementPainter(Graphics graphics)
		{
			this.graphics = graphics;
			this.graphics.CompositingQuality = CompositingQuality.HighQuality;
			this.graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
			this.graphics.SmoothingMode = SmoothingMode.HighQuality;
		}

		/// <summary>
		/// 在当前画布上画一个点
		/// </summary>
		/// <param name="point">传入点对象</param>
		public void drawPoint(Point point)
		{
			// 点的直径
			int sideLength = 4;
			// 绘制点中心
			Point center = new Point(point.X - sideLength / 2, point.Y - sideLength / 2);
			Rectangle rectangle = new Rectangle(center, new Size(sideLength, sideLength));
			// 绘制圆点
			graphics.FillEllipse(pointBrush, rectangle);
		}

		/// <summary>
		/// 在当前画布上画一条线
		/// </summary>
		/// <param name="lineSegment">线段</param>
		public void drawLine(LineSegment lineSegment)
		{
			graphics.DrawLine(linePen, lineSegment.Start, lineSegment.End);
		}

		/// <summary>
		/// 在当前画布上画一个多边形
		/// </summary>
		/// <param name="polygon">传入多边形</param>
		public void drawPolygon(Polygon polygon)
		{
			graphics.DrawPolygon(linePen, polygon.Points);
		}
	}
}